home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcr / pcr4_4.lha / DIST / debugnub / CirioNubStoreBytesToText.c < prev    next >
C/C++ Source or Header  |  1991-06-17  |  5KB  |  181 lines

  1. /* begincopyright
  2.   Copyright (c) 1991 Xerox Corporation. All rights reserved.
  3.   Use and copying of this software and preparation of derivative works based
  4.   upon this software are permitted. Any distribution of this software or
  5.   derivative works must comply with all applicable United States export
  6.   control laws. This software is made available AS IS, and Xerox Corporation
  7.   makes no warranty about the software, its performance or its conformity to
  8.   any specification. Any person obtaining a copy of this software is requested
  9.   to send their name and post office or electronic mail address to:
  10.     PCR Coordinator
  11.     Xerox PARC
  12.     3333 Coyote Hill Rd.
  13.     Palo Alto, CA 94304
  14.   endcopyright */
  15.  
  16. /*
  17.  * CirioNubStoreBytesToText.c
  18.  *
  19.  * Demers, June 6, 1991 2:49:24 pm PDT
  20.  */
  21.  
  22. /*
  23.     NOTE: This doesn't need to use ptrace -- it could instead
  24.     issue vp orders to get each vp to write-enable that part
  25.     of the text segment it needs to write in.  The semantics
  26.     would still be copy-on-write, the code would be somewhat
  27.     more complex.  The result would work on SVR4, but not in AIX.
  28.     So much for standards.
  29. */
  30.  
  31. #include "xr/Errno.h"
  32. #include "xr/ThreadsBackdoor.h"
  33. #include "xr/CirioNubProtocol.h"
  34. #include "xr/CirioNubMarshall.h"
  35. #include "xr/CirioNubProcs.h"
  36. #include "xr/CirioNubEnvironment.h"
  37.  
  38. #include "xr/ThreadsMsg.h"
  39.  
  40. #include <signal.h>
  41. #include <sys/ptrace.h>
  42. #include <sys/wait.h>
  43.  
  44. #define    DEBUG_STORE 1
  45. #define    DEBUG_FETCH 1
  46. #undef    DEBUG_FETCH
  47.  
  48. extern int etext;
  49.  
  50. #define TEXTSTART    ((char *)(8*1024))
  51. #define TEXTLIM        ((char *)(&etext))
  52.  
  53. bool
  54. CirioNubIsTextAddress(addr)
  55.     char *addr;
  56. {
  57.     if( addr < TEXTSTART ) return FALSE;
  58.     if( addr >= TEXTLIM) return FALSE;
  59.     return TRUE;
  60. }
  61.  
  62.  
  63.  
  64. CirioNubRetCode
  65. CirioNubStoreBytesToVPTextSegments(bytes, addr, nBytes)
  66.     char *bytes;
  67.     char *addr;
  68.     int nBytes;
  69. {
  70.     int ans, status, i;
  71.     int pid = 0;
  72.  
  73.     if( !CirioNubIsTextAddress(addr) ) {
  74.         return CirioNubPutInt32(-1);
  75.     }
  76.  
  77.     for( i = 0; i < XR_sysArea->sa_numVP; i++ ) {
  78.         pid = XR_sysArea->sa_vpe[i].vpe_pid;
  79.         ans = ptrace(PTRACE_ATTACH, pid, 0, 0, 0);
  80.         if( ans == (-1) ) {
  81. #           ifdef DEBUG_STORE
  82.                 XR_ConsoleMsg("Can't attach pid %d err %d\n", pid, errno);
  83. #           endif
  84.             pid = 0;  goto Bad;
  85.         }
  86.         for(;;) {
  87.             ans = waitpid(pid, &status, WUNTRACED);
  88.             if( ans != (-1) ) break;
  89.             if( errno == ECHILD ) {
  90. #               ifdef DEBUG_STORE
  91.                     XR_ConsoleMsg("Can't wait pid %d err %d\n", pid, errno);
  92. #               endif
  93.                 goto Bad;
  94.             }
  95.         }
  96.         ans = ptrace(PTRACE_WRITETEXT, pid, addr, nBytes, bytes);
  97.         if( ans == (-1) ) {
  98. #           ifdef DEBUG_STORE
  99.                 XR_ConsoleMsg("Can't write pid %d err %d\n", pid, errno);
  100. #           endif
  101.             goto Bad;
  102.         }
  103.         ans = ptrace(PTRACE_DETACH, pid, ((int *)(1)), SIGUSR1, 0);
  104.         if( ans == (-1) ) {
  105. #           ifdef DEBUG_STORE
  106.                 XR_ConsoleMsg("Can't detach pid %d err %d\n", pid, errno);
  107. #           endif
  108.             pid = 0; goto Bad;
  109.         }
  110.     }
  111.     return CirioNubPutInt32(0);
  112.  
  113.   Bad:
  114.     if( pid != 0 ) {
  115.         (void)ptrace(PTRACE_DETACH, pid, ((int *)(1)), SIGUSR1, 0);
  116.     }
  117.     return CirioNubPutInt32(-1);
  118. }
  119.  
  120.  
  121.  
  122. int
  123. CirioNubFetchBytesFromVPTextSegment(bytes, addr, nBytes, vpi)
  124.     char *bytes;
  125.     char *addr;
  126.     int nBytes;
  127.     int vpi;
  128. {
  129.     int ans, status;
  130.     int pid;
  131.  
  132.     if( !CirioNubIsTextAddress(addr) ) {
  133.         return (-1);
  134.     }
  135.  
  136.     {
  137.         pid = XR_sysArea->sa_vpe[vpi].vpe_pid;
  138.         ans = ptrace(PTRACE_ATTACH, pid, 0, 0, 0);
  139.         if( ans == (-1) ) {
  140. #           ifdef DEBUG_FETCH
  141.                 XR_ConsoleMsg("Can't attach pid %d err %d\n", pid, errno);
  142. #           endif
  143.             pid = 0; goto Bad;
  144.         }
  145.         for(;;) {
  146.             ans = waitpid(pid, &status, WUNTRACED);
  147.             if( ans != (-1) ) break;
  148.             if( errno == ECHILD ) {
  149. #               ifdef DEBUG_FETCH
  150.                     XR_ConsoleMsg("Can't wait pid %d err %d\n", pid, errno);
  151. #               endif
  152.                 goto Bad;
  153.             }
  154.         }
  155.         ans = ptrace(PTRACE_READTEXT, pid, addr, nBytes, bytes);
  156.         if( ans == (-1) ) {
  157. #           ifdef DEBUG_FETCH
  158.                 XR_ConsoleMsg("Can't read pid %d err %d\n", pid, errno);
  159. #           endif
  160.             goto Bad;
  161.         }
  162.         ans = ptrace(PTRACE_DETACH, pid, ((int *)(1)), SIGUSR1, 0);
  163.         if( ans == (-1) ) {
  164. #           ifdef DEBUG_FETCH
  165.                 XR_ConsoleMsg("Can't detach pid %d err %d\n", pid, errno);
  166. #           endif
  167.             pid = 0;  goto Bad;
  168.         }
  169.     }
  170.     return 0;
  171.  
  172.   Bad:
  173.     if( pid != 0 ) {
  174.         (void) ptrace(PTRACE_DETACH, pid, ((int *)(1)), SIGUSR1, 0);
  175.     }
  176.     return (-1);
  177. }
  178.  
  179.  
  180.  
  181.